package com.mayisoftware.mayioa.business.human.impl.controller;


import com.mayisoftware.mayioa.business.human.api.domain.HCBoardInfo;
import com.mayisoftware.mayioa.business.human.api.domain.HCMonitorInfo;
import com.mayisoftware.mayioa.business.human.api.domain.HumanSearchInfo;
import com.mayisoftware.mayioa.business.human.api.service.IHCService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.impl.config.Global;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.util.FileUploadUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户信息
 * 
 *
 */
@Controller
@RequestMapping("/human/hc")
public class HCController extends BaseController
{
    private static final Logger log = LoggerFactory.getLogger(HCController.class);

    private String prefix = "human/hc";

    @Autowired
    private IHCService hcService;
    /**
     * 词典服务
     */
    @Autowired
    ISysDictDataService sysDictDataService;

    @RequiresPermissions("human:hc:view")
    @GetMapping()
    public String staff()
    {
        log.error("TAG","sfasdasdfasdf");
        return prefix + "/hc";
    }

    @RequiresPermissions("human:hc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HumanSearchInfo searchInfo)
    {
        startPage();
        List<HCBoardInfo> list = hcService.getHCInfoByConditions(searchInfo);
        return getDataTable(list);
    }

    /**
     * 保存头像
     */
    @Log(title = "录入截图", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadfile")
    @ResponseBody
    public AjaxResult uploadfile(@RequestParam(name = "hcId") String hcId, @RequestParam("file") MultipartFile file)
    {
        try
        {
            if (!file.isEmpty())
            {
                String fileName = file.getOriginalFilename();
                String savePath = Global.getHCGoosePath();
                String planData = FileUploadUtils.upload(savePath, file);
//                SysUser user = ShiroUtils.getSysUser();
                log.info("上传文件:"+planData);
                String filePath = savePath+planData;


//                modelAndView.addObject("gooseFileName",planData);
                return  AjaxResult.success(planData);
            }
            return toAjax(0);
        }
        catch (Exception e)
        {
            log.error("上传文件失败！", e);
            return toAjax(0);
        }
    }


    @GetMapping("/edit/{hcId}")
    public String edit(@PathVariable("hcId") String hcId, ModelMap mmap)
    {
        mmap.put("hcInfo", hcService.getHCInfoById(hcId));
        return prefix + "/edit";
    }

    /**
     * 修改HC信息
     */
    @RequiresPermissions("human:hc:edit")
    @Log(title = "HC到位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(HCMonitorInfo hcMonitorInfo)
    {
        int records = 0;
        try{
//            hcMonitorInfo.setHcId(hcId);
//            hcMonitorInfo.setGoosePath(filePath);
//            hcService.updateHCMonitor(hcMonitorInfo);

            records = hcService.updateHCMonitor(hcMonitorInfo);

        }catch (Exception ex){
            return toAjax(0);
        }

        return  toAjax(records);

    }
}